<?php

return function ($oRes, $db_fadm, $method, $dictType, $dictCode, $pageNum, $pageSize,
        $dictLabel, $dictValue, $dictSort, $status, $remark) {
    $aWhere = array();
    $aWhere['dict_code'] = $dictCode;
    $oSql = $db_fadm->sql()->table('system_dict_data');
    switch ($method) {
        case('getdicts'):
            $aWhere = array();
            $aWhere['dict_type'] = $dictType;
            $oSql->where($aWhere);
            $aRows = $oSql->select();
            $oRes->data('data', $this->toCamel($aRows));
            break;
        case('list'):
            //$a = '{"total":3,"rows":[{"searchValue":null,"createBy":"admin","createTime":"2018-03-16 11:33:00","updateBy":null,"updateTime":null,"remark":"性别男","dataScope":null,"params":{},"dictCode":1,"dictSort":1,"dictLabel":"男","dictValue":"0","dictType":"sys_user_sex","cssClass":"","listClass":"","isDefault":"Y","status":"0","default":true},{"searchValue":null,"createBy":"admin","createTime":"2018-03-16 11:33:00","updateBy":null,"updateTime":null,"remark":"性别女","dataScope":null,"params":{},"dictCode":2,"dictSort":2,"dictLabel":"女","dictValue":"1","dictType":"sys_user_sex","cssClass":"","listClass":"","isDefault":"N","status":"0","default":false},{"searchValue":null,"createBy":"admin","createTime":"2018-03-16 11:33:00","updateBy":null,"updateTime":null,"remark":"性别未知","dataScope":null,"params":{},"dictCode":3,"dictSort":3,"dictLabel":"未知","dictValue":"2","dictType":"sys_user_sex","cssClass":"","listClass":"","isDefault":"N","status":"0","default":false}],"code":200,"msg":null}';
            //$data = json_decode($a, true);
            $aWhere = array();
            $aWhere['dict_type'] = $dictType;
            $oSql->where($aWhere);
            if (TRUE) {
                $page_num = intval($pageNum);
                if ($page_num < 1) {
                    $page_num = 1;
                }
                $oSql->limit($pageSize, $pageSize * ($page_num - 1));
            }
            $aRows = $oSql->select();
            $oRes->data('rows', $this->toCamel($aRows));
            $oRes->data('total', $oSql->count());
            break;
        case('get'):
            $mRow = $oSql->where($aWhere)->find();
            //$a = '{"msg":"操作成功","code":200,"data":{"searchValue":null,"createBy":"admin","createTime":"2018-03-16 11:33:00","updateBy":null,"updateTime":null,"remark":"性别未知","dataScope":null,"params":{},"dictCode":3,"dictSort":3,"dictLabel":"未知","dictValue":"2","dictType":"sys_user_sex","cssClass":"","listClass":"","isDefault":"N","status":"0","default":false}}';
            //$data = json_decode($a, true);
            $oRes->data('data', $this->toCamel($mRow));
            break;
        case('post'):
        case('put'):
            $mData = array();
            $mData['dict_label'] = $dictLabel;
            $mData['dict_value'] = $dictValue;
            $mData['dict_sort'] = $dictSort;
            $mData['status'] = $status;
            $mData['remark'] = $remark;
            $mData['dict_type'] = $dictType;
            if ($method === 'post') {
                // 添加
                $mData['create_time'] = 'CURRENT_TIMESTAMP()';
                $oSql->add($mData);
                $oRes->assign('msg', '添加成功');
            } else {
                // 修改
                $aWhere = array();
                $aWhere['dict_code'] = $dictCode;
                $mData['update_time'] = 'CURRENT_TIMESTAMP()';
                $oSql->where($aWhere)->save($mData);
                $oRes->assign('msg', '修改成功');
            }
            $db_fadm->commit();
            break;
    }
};
